/**
 * Created by yangfan on 2016/11/23.
 */

define(['config/appregister',"i18n","jquery","business/myResume/services/httpServices","business/services/utils"],
function(app,i18n,$,httpService,utils){

    app.directive("regionQuery",function(){
        return {
            templateUrl:"app/business/services/comComponents/region.html",
            scope:{
                conf:"=confData"
            },
            link:function(scope,element,attrs){
                
               //此作用域之所以变化  皆始于isShow
                scope.$watch("conf.isShow",function(newValue,oldValue){
                    if(newValue == true&&newValue!=oldValue){
                        var screenWidth = window.screen.availWidth;
              		    var divWidth = $(".inner-conatiner")[0].scrollWidth;
               	        var a =  $(".inner-conatiner").css("left",(screenWidth-divWidth)/2);
                        //scope.conf.reValue = [];
                        init()
                    }
                });
                scope.currentSelect = {
                	provinceId:"",
                	regionId:"",
                	areaId:"",
                	regionName:""
                };
                function reSetCurrentSelect (){
                	scope.currentSelect = {
                	provinceId:"",
                	regionId:"",
                	areaId:"",
                	provinceName:"",
                	regionName:"",
                	areaName:""
               	 };
                }
         	           
                scope.chooseOk = function(){
                     scope.conf.isShow = false;
                    scope.conf.showValue = utils.getShowValue(scope.conf.reValue,"regionName");
                     //scope.$emit("334",{})
                };
                scope.chooseCancel = function(){
                     scope.conf.isShow = false;
                     
                };
                scope.showCityList = [];
                   var rgbProvice = "rgb(254,156,73)",
                 	   rgbCity = "rgb(254,156,73)",
                 	   proviceTempColor,
                 	   cityTempColor;
                proviceTempColor = new dealColor();                 	 
                cityTempColor = new dealColor();
               function dealColor(){
               	var marCount = 0,marDoc;
               	return {
               		singleInstance:function(event,item,rgb){
               			 if(event.target.style.color!=rgb){
                        if(marCount ==0){
                            marDoc = event.target;
                           event.target.style.color=rgb;
              
                        }else{
                            marDoc.style.color = "";
                            marDoc = event.target;
                            event.target.style.color=rgb;
                        }
                        marCount++;
                    } else if(event.target.style.color==rgb){
//                      event.target.style.color="";
                    	}
               		},
               		clearMarDoc :function(){
                        if(marDoc){
                            marDoc.style.color = "";
                        }
               		}
               		
               	}
               
                };
                var curProviceIndex = 0;
               
                scope.clickProvice = function(event,item,index){
                              	
                	proviceTempColor.singleInstance(event,item,rgbProvice);
                	scope.currentSelect = {
                	provinceId:"",
                	regionId:"",
                	areaId:"",
                	provinceName:"",
                	regionName:"",
                	areaName:""
               	 };
                    cityTempColor.clearMarDoc();
               	    scope.currentSelect.provinceName = item.provinceName;
                	scope.currentSelect.provinceId = item.provinceId;  
                    scope.showCityList = [];
                    scope.showAteaList = [];
                    scope.showHotAteaList = [];
                    scope.showCityList[index] = true;
                    getRegionListInProvince(item);
                };
                scope.showAteaList = [];
                scope.clickCity = function(event,item,index){
                	scope.currentSelect.regionName = item.regionName; 
                	scope.currentSelect.regionId = item.regionId;
                	cityTempColor.singleInstance(event,item,rgbCity);               	                       	               
                    scope.showAteaList = [];
                    scope.showAteaList[index] = true;
                    getAreaListInRegionId(item);

                };
                scope.clickHotCitys = function(item,index,event){
                    scope.hotAteaIndex = index;
                	proviceTempColor.clearMarDoc();
                	cityTempColor.singleInstance(event,item,rgbCity);
                	reSetCurrentSelect ();
                    scope.showHotAteaList = [];
                    scope.showAteaList = [];
                    scope.showCityList = [];
                    scope.showHotAteaList[index] = true;
                    scope.currentSelect.provinceId = item.provinceId;          
                	scope.currentSelect.provinceName = item.provinceName;          
                	scope.currentSelect.regionName = item.isHotName; 
                	scope.currentSelect.regionId = item.regionId;
                    getAreaListInRegionId(item);

                };
                scope.clickArea = function(item){
                	
                	scope.currentSelect.areaId = item.areaId;
                	scope.currentSelect.areaName = item.areaName;
                	setModel();
                	 scope.showHotAteaList = [];
                    scope.showAteaList = [];
                    scope.showCityList = [];
                    proviceTempColor.clearMarDoc();
                    cityTempColor.clearMarDoc();
                };
                scope.removeCity = function(item){
                	scope.conf.reValue = utils.removeArray(scope.conf.reValue,item)
                	 
                };
                function setModel(){
                	if(!scope.conf.hasOwnProperty("maxSelect")){
                		return 
                	}
                    if(!angular.isArray(scope.conf.reValue )){
                        return
                    };
                    var len = scope.conf.reValue.length;
                    if(len< scope.conf.maxSelect){
                        scope.conf.reValue.push(scope.currentSelect);
                    }else{
                        scope.currentSelect = {};
                        alert("最多只能选择"+len+"项");
                    }
                }
                function getHotRegionList(){
                    var req = {
                        type:"GET",
                        uri:"getHotRegionList"
                    };
                    httpService.getProvince(req).success(function(response){
                        scope.$apply(function(){
                            scope.HotRegionList = response.data;
                        })
                    })
                };
                function procvices(){
                    var req = {
                        type:"GET",
                        uri:"getProvinceList"
                    };
                    httpService.getProvince(req).success(function(response){
                        scope.$apply(function(){
                            scope.procvices = response.data;
                        })
                    })
                };
                scope.param = {
                    province:"",
                    city:"",
                    area:""
                };
                function getRegionListInProvince(item){
                    var req = {
                        type:"GET",
                        uri:"getRegionListInProvince",
                        data:{
                            provinceId:item.provinceId
                        }
                    };
                    httpService.getProvince(req).success(function(response){
                        scope.$apply(function(){
                            scope.RegionListInProvince = response.data;
                        })
                    })
                };
                function getAreaListInRegionId(item){
                    var req = {
                        type:"GET",
                        uri:"getAreaListInRegionId",
                        data:{
                            regionId:item.regionId
                        }
                    };
                    httpService.getProvince(req).success(function(response){
                        scope.$apply(function(){
                            scope.RegionListInCity = response.data;
                        })
                    })
                };
                function init(){
                    getHotRegionList();
                    procvices();
                }

                
             
            }
        }
    }).directive("jobQuery",function(){
        return {
            templateUrl:"app/business/services/comComponents/job.html",
            scope:{
                conf:"=confData"
            },
            link:function(scope,element,attrs){
                var reArr = [];
                scope.curTop = "";

                scope.$watch("conf.isShow",function(newValue,oldValue){
                    if(newValue == true&&newValue!=oldValue){

                        var screenWidth = window.screen.availWidth;
                        var divWidth = $(".inner-conatiner")[0].scrollWidth;
                        var a =  $(".inner-conatiner").css("left",(screenWidth-divWidth)/2);
                            init();
                    }
                });
                scope.chooseOk = function(){
                    scope.conf.isShow = false;
                 //   scope.conf.top = scope.selectTop;
                    scope.conf.detail = scope.selectCategory;
                        //scope.conf.showValue = scope.selectCategory[0].fdcName;
                    scope.conf.showValue =utils.getShowValue(scope.selectCategory,"fdcName");

                };
                scope.chooseCancel = function(){
                    scope.conf.isShow = false;
                };
                scope.removeItem = function(item){
                  scope.selectCategory = utils.removeArray(scope.selectCategory,item);
                };
                scope.getCurrentSelectStyle = function(item){
                  var id = item.fdcId;
                  var ret ="";
                    scope.selectCategory.forEach(function(item,index){
                       if (item.fdcId == id){
                            ret = {"color":"#00AAFE"};
                           return
                       }
                    });
                    return ret
                };
                //scope.selectTop = "";


                scope.clickTop = function(item){
                    scope.curFtcId = item.ftcId;
                    scope.curTop = item;
                    //scope.selectTop = item;
                    getFuncDetailCategory();
                 //   scope.selectCategory = [];
                };
                //  detail 配色 ，select 项显示
                scope.clickDetail = function(event,item){
                    if(event.target.style.color!="rgb(0, 170, 254)"){
                        if(scope.selectCategory.length<scope.conf.maxSelect){
                            event.target.style.color="rgb(0, 170, 254)";
                            scope.selectCategory.push(item);
                        } else {
                            alert("职能只能够选择"+scope.conf.maxSelect+"项");
                            return
                        }
                    }else if(event.target.style.color=="rgb(0, 170, 254)"){
                        event.target.style.color="";
                        var key = "fdcId";
                        scope.selectCategory = utils.removeArrayKey(scope.selectCategory,item,key);
                    }
                };
                function getFunctionsTopCategory(){
                    var req = {
                        type:"GET",
                        uri:"getFunctionsTopCategory",
                    };
                    httpService.jobServices(req).success(function(response){
                        scope.$apply(function(){
                            scope.jobTopCategory= response.data;
                        });
                        scope.curFtcId = scope.jobTopCategory[0].ftcId;
                       scope.curTop = scope.jobTopCategory[0];
                        getFuncDetailCategory()
                    }).error(function(){})
                }
                function getFuncDetailCategory(){
                    var req = {
                        type:"GET",
                        uri:"getFuncDetailCategory",
                        data:{
                            ftcId:scope.curFtcId
                        }
                    };
                    httpService.jobServices(req).success(function(response){
                        scope.$apply(function(){
                            scope.jobDetailCategory= response.data;
                        });
                    }).error(function(){})
                }

                function init (){
                    var maxSelect = scope.conf.maxSelect;
                    scope.selectCategory = [];
                    if(toString.call(scope.conf.detail)=='[object Array]'){
                        scope.selectCategory = scope.conf.detail;
                    }else if(toString.call(scope.conf.detail)=='[object Object]'){
                        scope.selectCategory.push(scope.conf.detail);
                    }else{
                        scope.selectCategory = [];
                    }

                    getFunctionsTopCategory()
                }


            }
        }
    }).directive("majorQuery",function(){
        return {
            templateUrl:"app/business/services/comComponents/major.html",
            scope:{
                conf:"=confData"
            },
            link:function(scope,element,attrs){
                var reArr = [];
                scope.chooseOk = function(){
                    scope.conf.isShow = false;
                    scope.conf.top = scope.selectTop;
                    scope.conf.second = scope.selectCategory;
                    scope.conf.detail = scope.selectDetail;           
                };
                scope.chooseCancel = function(){
                    scope.conf.isShow = false;
                };
                scope.selectTop = "";
                scope.selectCategory = "";
                scope.selectDetail = scope.conf.detail;
                scope.showDetail =[];
                scope.clickTop = function(item){
                    scope.selectTop = item;
                    getMajorScByMdcId(item.mdcId);
                    scope.selectCategory = "";
                    scope.showDetail = [];
                };
                scope.offsetStyle = {
                    position:"absolute",
                    top:"0",
                    left:"5px"
                };
                var count = 0,doc;
                scope.clickSec = function(event,item,index,p_index){
                    
                    if(index == 1){
                        scope.offsetStyle.left = "210px";
                    }else if(index == 2){
                        scope.offsetStyle.left = "420px";
                    }else if( index == 0){
                        scope.offsetStyle.left = "5px";
                    }
                    if(event.target.style.color!="rgb(0, 170, 254)"){
                        if(count ==0){
                            doc = event.target;
                            event.target.style.color="rgb(0, 170, 254)";
                            scope.selectCategory = item;

                        }else{
                            doc.style.color = "";
                            doc = event.target;
                            event.target.style.color="rgb(0, 170, 254)";
                            scope.selectCategory = item;
                        }
                        count++;
                    } else if(event.target.style.color=="rgb(0, 170, 254)"){
                        event.target.style.color="";
                        scope.selectCategory = "";
                    }
                    var curMscId = item.mscId;
                    getProfessionalByMscId(curMscId,p_index);
                };
                var marCount = 0,marDoc;
                scope.clickMarjorItem = function(event,item){
                    if(event.target.style.color!="rgb(0, 170, 254)"){
                        if(marCount ==0){
                            marDoc = event.target;
                            event.target.style.color="rgb(0, 170, 254)";
                            scope.selectDetail = item;

                        }else{
                            marDoc.style.color = "";
                            marDoc = event.target;
                            event.target.style.color="rgb(0, 170, 254)";
                            scope.selectDetail = item;
                        }
                        marCount++;
                    } else if(event.target.style.color=="rgb(0, 170, 254)"){
                        event.target.style.color="";
                        scope.selectDetail = "";
                    }
                };
                scope.removeItem = function(){
                    scope.selectDetail = "";
                };
                scope.isShowcurrentSelect =function(){
                  if(toString.call(scope.selectDetail) =='[object Object]' ){
                      return true
                  }else{
                      return false
                  }
                };
                function shiftArray(arr){
                    var reArr = []; reArr[0] = [];
                    var j = 0;
                    for(var i=0;i<arr.length;i++){
                        reArr[j].push(arr[i]);
                        if((i+1)%3==0){
                            j++;
                            reArr[j] = [];
                        };
                    }
                    return reArr;
                };
                function getMajorTopCategory(){
                    var req = {
                        type:"GET",
                        uri:"getMajorTopCategory",
                    };
                    httpService.jobServices(req).success(function(response){
                        scope.$apply(function(){
                            scope.majorTopCategory= response.data;
                        });
                        scope.curMdcId = scope.majorTopCategory[0].mdcId;
                        scope.selectTop = scope.majorTopCategory[0];
                        getMajorScByMdcId(scope.curMdcId)
                    }).error(function(){})
                }
                function getMajorScByMdcId(curMdcId){
                    var req = {
                        type:"GET",
                        uri:"getMajorScByMdcId",
                        data:{
                            mdcId:curMdcId
                        }
                    };
                    httpService.jobServices(req).success(function(response){
                        scope.$apply(function(){
                            scope.majorSecCategory= response.data;
                            scope.tableData = shiftArray(scope.majorSecCategory);
                        });
                    }).error(function(){})
                }
                function getProfessionalByMscId(curMscId,p_index){
                    var req = {
                        type:"GET",
                        uri:"getProfessionalByMscId",
                        data:{
                            mscId:curMscId
                        }
                    };
                    httpService.jobServices(req).success(function(response){
                        scope.$apply(function(){
                            scope.majorProfessionCategory= response.data;
                            scope.showDetail = [];
                            scope.showDetail[p_index] = true;
                        });
                    }).error(function(){})
                }
                getMajorTopCategory()


            }
        }
    }).directive("industryQuery",function(){
        return {
            templateUrl:"app/business/services/comComponents/industry.html",
            scope:{
                conf:"=confData"
            },
            link:function(scope,element,attrs){
                var reArr = [];
                scope.$watch("conf.isShow",function(newValue,oldValue){
                    if(newValue == true&&newValue!=oldValue){

                        var screenWidth = window.screen.availWidth;
                        var divWidth = $(".inner-conatiner")[0].scrollWidth;
                        var a =  $(".inner-conatiner").css("left",(screenWidth-divWidth)/2);
                            init();
                    }
                });
                
                scope.chooseOk = function(){
                    scope.conf.isShow = false;
                    scope.conf.detail = scope.selectCategory;
                    var len = scope.selectCategory.length;
                    //scope.conf.showValue = "已选择"+ len + "项";
                    scope.conf.showValue =utils.getShowValue(scope.selectCategory,"dictName");
                };
                scope.chooseCancel = function(){
                    scope.conf.isShow = false;
                };
                scope.removeItem = function(item){
                    scope.selectCategory = utils.removeArray(scope.selectCategory,item);
                };
                scope.getCurrentSelectStyle = function(item){
                    var id = item.dictId;
                    var ret ="";
                    scope.selectCategory.forEach(function(item,index){
                        if (item.dictId == id){
                            ret = {"color":"#00AAFE"};
                            return
                        }
                    });
                    return ret
                };
                //scope.selectTop = "";


                scope.clickDetail = function(event,item){
                    if(event.target.style.color!="rgb(0, 170, 254)"){
                        if(scope.selectCategory.length<scope.conf.maxSelect){
                            event.target.style.color="rgb(0, 170, 254)";
                            scope.selectCategory.push(item);
                        } else {
                            alert("行业只能够选择"+scope.conf.maxSelect+"项");
                            return
                        }
                    }else if(event.target.style.color=="rgb(0, 170, 254)"){
                        event.target.style.color="";
                        scope.selectCategory = utils.removeArrayKey(scope.selectCategory ,item,"dictId");
                       // scope.selectCategory.remove(item);
                    }
                };
                function getCompanyGuild(){
                    var req = {
                        type:"GET",
                        uri:"getCompanyGuild",
                    };
                    httpService.getCvdatabases(req).success(function(response){
                        scope.$apply(function(){
                            scope.guild= response.data;
                        });                     
                    }).error(function(){})
                }                

                function init(){
                     scope.conf.maxSelect;
                    scope.selectCategory = [];
                    if(toString.call(scope.conf.detail)=='[object Array]'){
                        scope.selectCategory = scope.conf.detail;
                    }else if(toString.call(scope.conf.detail)=='[object Object]'){
                        scope.selectCategory.push(scope.conf.detail);
                    }else{
                        scope.selectCategory = [];
                    }
                    getCompanyGuild()
                }
            }
        }
    }).directive("textBox",function(){
        return {
            templateUrl:"app/business/services/comComponents/textbox.html",
            scope:{
                length:"=confLength",
                content:"=confContent"
            },
            link:function(scope,element,attr){
                scope.curLength =utils.getContentLength(scope.content);
                scope.$watch("content",function(newValue,oldValue){
                    if(newValue!= oldValue){
                        scope.curLength = utils.getContentLength(scope.content);

                    }
                });
                scope.getCurentStyle = function(type){
                    if(parseInt(scope.curLength)>parseInt(scope.length)){
                        if(type == "msg"){
                            return {
                                color:"red"
                            }
                        }
                        if(type == "border"){
                            return {
                                border:"1px solid red"
                            }
                        }

                    }else {
                        return ""
                    }
                }
            }
        }
    })
});